# FindRivsFFmpeg.cmake
# Usage: find_package(RivsFFmpeg REQUIRED)
# Provides:
#   RivsFFmpeg_FOUND
#   RivsFFmpeg_INCLUDE_DIRS
#   RivsFFmpeg_LIBRARIES
#   Imported target: Rivs::FFMPEG (INTERFACE)
#
# Hints:
#   -DRIVS_ROOT=/opt/rivs (or set environment RIVS_ROOT)
# Defaults (if no hints provided):
#   Include dir: /opt/rivs/include/ffmpeg
#   Lib dir:     /opt/rivs/lib

# Default RIVS_ROOT (if not provided)
if(NOT RIVS_ROOT)
  if(DEFINED ENV{RIVS_ROOT} AND NOT "$ENV{RIVS_ROOT}" STREQUAL "")
    set(RIVS_ROOT "$ENV{RIVS_ROOT}" CACHE PATH "Root prefix of Rivs installation" FORCE)
  else()
    set(RIVS_ROOT "/opt/rivs" CACHE PATH "Root prefix of Rivs installation" FORCE)
  endif()
endif()
message(STATUS "[FindRivsFFmpeg] RIVS_ROOT=${RIVS_ROOT}")

set(_RIVS_HINT_PREFIXES
    ${RIVS_ROOT}
    $ENV{RIVS_ROOT}
)

# Build include/lib hint lists
set(_FFMPEG_INCLUDE_HINTS /opt/rivs/include/ffmpeg)
set(_FFMPEG_LIB_HINTS /opt/rivs/lib)
foreach(pfx IN LISTS _RIVS_HINT_PREFIXES)
  if(pfx)
    list(APPEND _FFMPEG_INCLUDE_HINTS
      ${pfx}/include/ffmpeg
      ${pfx}/usr/include/ffmpeg
      ${pfx}/opt/rivs/include/ffmpeg
    )
    list(APPEND _FFMPEG_LIB_HINTS
      ${pfx}/lib
      ${pfx}/usr/lib
      ${pfx}/opt/rivs/lib
    )
  endif()
endforeach()

# Try to locate Rivs-provided FFmpeg first
# Find include dir (we look for a common header like libavcodec/avcodec.h)
find_path(RivsFFmpeg_INCLUDE_DIR
  NAMES libavcodec/avcodec.h
  HINTS ${_FFMPEG_INCLUDE_HINTS}
)

# Find libs
find_library(RivsFFmpeg_AVCODEC    NAMES avcodec    HINTS ${_FFMPEG_LIB_HINTS})
find_library(RivsFFmpeg_AVFORMAT   NAMES avformat   HINTS ${_FFMPEG_LIB_HINTS})
find_library(RivsFFmpeg_AVFILTER   NAMES avfilter   HINTS ${_FFMPEG_LIB_HINTS})
find_library(RivsFFmpeg_AVUTIL     NAMES avutil     HINTS ${_FFMPEG_LIB_HINTS})
find_library(RivsFFmpeg_SWSCALE    NAMES swscale    HINTS ${_FFMPEG_LIB_HINTS})
find_library(RivsFFmpeg_AVDEVICE   NAMES avdevice   HINTS ${_FFMPEG_LIB_HINTS})

set(_rivs_ffmpeg_libs
  ${RivsFFmpeg_AVCODEC}
  ${RivsFFmpeg_AVFORMAT}
  ${RivsFFmpeg_AVFILTER}
  ${RivsFFmpeg_AVUTIL}
  ${RivsFFmpeg_SWSCALE}
  ${RivsFFmpeg_AVDEVICE}
)

# If not all libs were found or include dir missing, try system FFmpeg via pkg-config
set(_rivs_ffmpeg_ok TRUE)
if(NOT RivsFFmpeg_INCLUDE_DIR)
  set(_rivs_ffmpeg_ok FALSE)
endif()
foreach(lib IN LISTS _rivs_ffmpeg_libs)
  if(NOT lib)
    set(_rivs_ffmpeg_ok FALSE)
  endif()
endforeach()

if(NOT _rivs_ffmpeg_ok)
  message(STATUS "[FindRivsFFmpeg] Rivs FFmpeg not fully found, trying system ffmpeg via pkg-config...")
  find_package(PkgConfig QUIET)
  if(PKG_CONFIG_FOUND)
    pkg_check_modules(FFMPEG QUIET IMPORTED_TARGET
      libavcodec libavformat libavfilter libavutil libswscale libavdevice)
    if(FFMPEG_FOUND)
      set(RivsFFmpeg_INCLUDE_DIRS ${FFMPEG_INCLUDE_DIRS})
      set(RivsFFmpeg_LIBRARIES ${FFMPEG_LIBRARIES})
      set(RivsFFmpeg_INCLUDE_DIR "${FFMPEG_INCLUDE_DIRS}")
      message(STATUS "[FindRivsFFmpeg] Using system ffmpeg via pkg-config")
    endif()
  endif()
endif()

# If Rivs-style search succeeded, populate standard variables
if(_rivs_ffmpeg_ok)
  set(RivsFFmpeg_INCLUDE_DIRS ${RivsFFmpeg_INCLUDE_DIR})
  set(RivsFFmpeg_LIBRARIES ${_rivs_ffmpeg_libs})
endif()

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(RivsFFmpeg
  REQUIRED_VARS RivsFFmpeg_INCLUDE_DIR RivsFFmpeg_LIBRARIES
)

if(RivsFFmpeg_FOUND)
  if(NOT TARGET Rivs::FFMPEG)
    add_library(Rivs::FFMPEG INTERFACE IMPORTED)
  endif()
  # Prefer include dirs list variable if set, else single include dir
  if(RivsFFmpeg_INCLUDE_DIRS)
    set_target_properties(Rivs::FFMPEG PROPERTIES
      INTERFACE_INCLUDE_DIRECTORIES "${RivsFFmpeg_INCLUDE_DIRS}")
  else()
    set_target_properties(Rivs::FFMPEG PROPERTIES
      INTERFACE_INCLUDE_DIRECTORIES "${RivsFFmpeg_INCLUDE_DIR}")
  endif()
  set_target_properties(Rivs::FFMPEG PROPERTIES
    INTERFACE_LINK_LIBRARIES      "${RivsFFmpeg_LIBRARIES}")

  message(STATUS "[FindRivsFFmpeg] Include dirs: ${RivsFFmpeg_INCLUDE_DIRS}")
  message(STATUS "[FindRivsFFmpeg] Libs: ${RivsFFmpeg_LIBRARIES}")
endif()
